using Gtk;

namespace Atlantis
{
	public class ProjectWizardView : GLib.Object, IInjectRegistry
	{
		Dialog window;
		Entry entryProjectName;
		Entry entryLocationName;
		Entry entrySolutionName;
		CheckButton create_solution_dir_checkbutton;
		Registry registry;
		
		
		public ProjectWizardView() {
			stderr.printf("new ProjectWizardView");
		}
		
		construct {
			stderr.printf("construct");
			Initialize();
		}
		
		public Registry Registry{
			get{
				return registry;
			}
			set{
				registry = value;
			}
		}
		
		public string ProjectName{
			get {
				return entryProjectName.text;
			}
			set {
				entryProjectName.text = value;
			}
		}
		
		public string SolutionName{
			get {
				return entrySolutionName.text;
			}
			set {
				entrySolutionName.text = value;
			}
		}
		
		public string LocationName{
			get {
				return entryLocationName.text;
			}
			set {
				entryLocationName.text = value;
			}
		}
		
		public bool CreateSeparateSolutionDirectory {
			get {
				return create_solution_dir_checkbutton.active;
			}
			set {
				create_solution_dir_checkbutton.active = value;
			}
		}
			
		private void Initialize(){
			stderr.printf("Initialize");
			var builder = new Builder();
            builder.add_from_file ("ui.glade");
            window = builder.get_object("ProjectWizardView") as Dialog;
            builder.connect_signals (this);
            
            entryProjectName = builder.get_object("projectName") as Entry;
            entryLocationName = builder.get_object("locationName") as Entry;
            entrySolutionName = builder.get_object("solutionName") as Entry;
            create_solution_dir_checkbutton = builder.get_object("checkbuttonCreateSolution") as CheckButton;
            stderr.printf("btnOk = ...");
            stderr.printf("btnCancel = ...");
		}
		
		[CCode(instance_pos=-1)]
		public void on_btnCancel_clicked(Widget sender){
			stderr.printf("handle_btnCancel_clicked");
			window.destroy();
			stderr.printf("exiting handle_btnCancel_clicked");
		}
		
		[CCode(instance_pos=-1)]
		public void on_btnOk_clicked(Widget sender){
			stderr.printf("handle_btnCancel_clicked");
			
			TemplateArgs args = new TemplateArgs();
			args.ProjectName = ProjectName;
			args.ProjectLocation = LocationName;
			args.SolutionName = SolutionName;
			args.CreateSeparateSolutionDirectory = CreateSeparateSolutionDirectory;
			
			GtkProjectTemplate template = new GtkProjectTemplate();
			var project = template.Generate(args);
			
			var fileName = LocationName + "/" + ProjectName + ".xml";
			var projectWriter = new ProjectWriter( fileName );
			projectWriter.write_project( project );
			stderr.printf( fileName );
			
			var agg = Registry.Get<IEventAggregator>();
            var message = new OpenProjectEvent() { Project = project } ;
            agg.send_message<OpenProjectEvent>(message);
            
			window.destroy();
		}
		
		public void Show(){
			//window.show_all();
			window.run();
		}
	}
}
